<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\controller\HomeBaseController;
use tree\Tree;
use think\facade\Db;

class IndexController extends HomeBaseController
{

    //变量调用    product_category_list
    // 'type'=>1  二级栏目和导航下对应列表（item.list）  
    // 'type'=>2  列表 
    // 'type'=>3  二级栏目
    //  protected $view_share_var = array(
    //     PRODCUT_CATEGORY_LIST => array( // 产品分类列表 ，对应控制器ProductController,  
    //         'position' => true,'type'=>1,'isSelect'=>true,'controller'=>'product'
    //     ), 
    //     NEWS_CATEGORY_LIST => array(//新闻分类列表,对应控制器NewsController
    //         'position' => false,'type'=>1,'isSelect'=>true,'controller'=>'news'
    //     ), 

    //     CASE_CATEGORY_LIST => array(//案例分类列表，对应控制器CaseController
    //         'position' => false,'type'=>2,'isSelect'=>true,'controller'=>'case'
    //     ),  
    //     PRODCUT_RECOMMEND_LIST => array( // 产品推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>true,'controller'=>'product'
    //     ),
    //     NEWS_RECOMMEND_LIST => array( // 新闻推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>false,'controller'=>'news'
    //     ),
    //     CASE_RECOMMEND_LIST => array( // 案例推荐列表 ，对应控制器ProductController,  
    //         'position' => false,'type'=>2,'isSelect'=>false,'controller'=>'case'
    //     ),        
    // );  
    
    
    // public $cache_time = '60';  // 首页接口数据缓存时间
    public function index()
    {       

        $cache_data = cache('PORTAL_INDEX_INFO');
        if(empty($cache_data)){
            $assign_data = array();
            $sub_list = array('product','case','portal','clientele');
            $field = 'id,more,post_title,published_time,post_excerpt';

            
            $sub_list = array(
                'product' => array('where' => array('post_status' => '1','delete_time'=>'0'),'order' =>'recommended desc,list_order asc','limit' => 9,'field'=>$field),
                'case' => array('where' => array('post_status' => '1','delete_time'=>'0'),'order' => 'recommended desc,list_order asc','limit' => 9,'field'=>$field),
                'portal' => array('where' => array('post_status' => '1','post_type'=>'1','delete_time'=>'0'),'order' => 'recommended desc','limit' => 9,'field'=>$field),       
            );

            foreach($sub_list as $key=>$val){
                $request_data = $val;                
                $request_data['table'] = empty($request_data['table'])? $key . '_post':$request_data['table'];    
               
                     
                    //getdata 方法针对所有表的非关联查询。
                    $assign_data[$key] = $this->getOrgData($request_data);
                            
                
            }             
            
            $assign_data['about'] = $this->getAbout();
            $assign_data['link'] = $this->getLink();
            
            cache('PORTAL_INDEX_INFO', json_encode($assign_data) , $this->cache_time);
        }else{
            $assign_data = json_decode($cache_data,TRUE);
        }

        $clientele=db('clientele')->order('list_order')->limit('12')->select()->toArray();
        foreach ($clientele as $key => &$value) {
           $value['more']=json_decode($value['more'],TRUE);
        }
       
        $this->assign('clientele',$clientele);
        

        $this->assign($assign_data);



        return $this->fetch(':index');
        
    }
    
    
    
    //获取公司简介
    public function getAbout(){
        $return_data = array();
        $about_us = Db::name('portal_post')
                ->field('post_excerpt,post_content,more')
                ->where('post_title','like','%关于%')
                ->where('post_type','2')
                ->find();
        $return_data['post_excerpt'] = $about_us['post_excerpt'];
        $return_data['post_content'] = cmf_replace_content_file_url(htmlspecialchars_decode($about_us['post_content']));
        $more = json_decode($about_us['more'],TRUE);
        unset($about_us['more']);
        $return_data['photo'] = $more['thumbnail'];
        return $return_data;
    }
    
    
    public function getOrgData($data){
    
       
        $return_data = Db::name($data['table'])
                ->field($data['field'])
                ->where($data['where'])
                ->order($data['order'])
                ->limit($data['limit'])
                ->select()
                ->toArray();

        foreach ($return_data as &$value){         
            if(!empty($value['more']))$value['more'] = json_decode($value['more'],TRUE);           
        }
           
        return $return_data;
        
    }
    
    
    
}
	